/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/3/14
 */
import React, {Component} from 'react';
import {Form,message,Input,Button} from "antd";
import {connect} from "react-redux";
import {hide, show} from "../../../configs/SharedReducer";
import {saveUserEmailAlert, findMyEmailAlert} from "../service";
import {getUserInfo} from "../../../commons/utils/CommonUtils";

const FormItem = Form.Item;
const formItemLayout = {
  labelCol: {span: 8},
  wrapperCol: {span: 12}
}
class EmailInfo extends Component {
  state = {
    initValue: null
  };

  componentDidMount() {
    this.getDataSource();
  }

  getDataSource = () => {
    const {userId} = this.props;
    this.props.show();
    findMyEmailAlert()
      .then(res => {
        if (res[0]){
          this.setState({initValue: res[0]});
        }
      })
      .finally(() => {
        this.props.hide();
      })
  }
  handleSave = () => {
    const {initValue} = this.state;
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        let params = {...initValue,...values}
        let userInfo=getUserInfo();
        if(!params.userId){
          params.userId=userInfo.userId;
        }
        if(!params.tenantCode){
          params.tenantCode=userInfo.tenantCode
        }
        this.props.show();
        saveUserEmailAlert(params).then(result => {
          if (result.status === "SUCCESS") {
            message.success(result.message ? result.message : "请求成功");
            //刷新本地数据
            this.getDataSource();
          } else {
            message.error(result.message ? result.message : "请求失败");
          }
        }).catch(e => {
        }).finally(() => {
          this.props.hide();
        })
      }
    })
  }

  render() {
    const {initValue} = this.state;
    const {getFieldDecorator} = this.props.form;
    return (
      <Form style={{width: 600}}>
        <FormItem
          label={"间隔时间(小时)"}
          {...formItemLayout}>
          {getFieldDecorator('hours', {
            initialValue: initValue && initValue.hours ? initValue.hours : "",
            rules: [{required: true, message: '请填写间隔时间(小时)!'}]
          })(
            <Input/>
          )}
        </FormItem>
        <FormItem
          label={"待办工作数量(个)"}
          {...formItemLayout}>
          {getFieldDecorator('toDoAmount', {
            initialValue: initValue ? initValue.toDoAmount : "",
            rules: [{required: true, message: '待办工作数量(个)!'}]
          })(
            <Input/>
          )}
        </FormItem>
        <FormItem
          label={"最后提醒时间"}
          {...formItemLayout}>
          {getFieldDecorator('lastTime', {
            initialValue: initValue ? initValue.lastTime : "",
          })(
            <Input disabled/>
          )}
        </FormItem>
        <br/>
        <div style={{width: "100%",display: "flex",justifyContent: "center"}}>
          <Button type={"primary"} onClick={this.handleSave}>保存</Button>
        </div>
      </Form>
    );
  }
}

export default connect(null,{show,hide})(Form.create()(EmailInfo));
